$(function () {
    var
        _user,											//用户信息
        _query = {"index": 0, "tip": "goods"},	//当前页
        _html = [],										//html字符串
        _myscroll,										//滚动
        _thispage,										//当前页
        _pullUp,
        _pullDown;

    var attentionPage = function () {
        this.init();
    };
    attentionPage.prototype = {
        init: function () {
            this.addScroll();
            this.loadData();
            this.bindEvent();
        },
        addScroll: function () {
            _pullUp = $("#pullUp");
            _pullDown = $("#pullDown");
            _myscroll = new IScroll('#wrapper', {
                probeType: 2,
                scrollbars: true,
                mouseWheel: false,
                fadeScrollbars: true,
                bounce: true,
                interactiveScrollbars: false,
                shrinkScrollbars: 'scale',
                click: true,
                keyBindings: true,
                momentum: true
            });
            _myscroll.on("scrollEnd", function () {
//          	console.log("this.y=" + this.y);
//          	console.log("this.maxScrollY=" + this.maxScrollY);
                if ((this.y < 0) && (this.y == this.maxScrollY)) {
                    console.log("滚动到底部");
                    _pullUp.show();
                    setTimeout(function () {
                        _query.index++;
                        _thispage.loadAttentions();
                    }, 500);
                } else if ((this.y == 0) && (this.y != this.maxScrollY)) {
                    console.log("滚动到顶部");
                    _pullDown.show();
                    setTimeout(function () {
                        _query.index--;
                        if (_query.index < 0) {
                            _query.index = 0;
                        }
                        ;
                        _thispage.loadAttentions();
                    }, 500);
                }
            });
        },
        //加载
        loadData: function () {
            _user = getLocalStorage(STORAGE_MANAGER.user);
            if (!_user) {
                redirect("../account/login.html");
                return false;
            } else {
                //alert( user.id );
            }
            ;
            this.loadAttentions();
        },
        loadAttentions: function () {
            var requestUrl = CONFIG_MANAGER.RequstUrl + "v1/member/favorite/";
            switch (_query.tip) {
                case "goods":
                    requestUrl += "goods/";
                    break;
                case "shop":
                    requestUrl += "shop/";
                    break;
                case "trace":
                    requestUrl += "goods/";
                    break;
                default:
                    break;
            }
            requestUrl += "{0}-{1}-{2}".format(_user.id, _query.index, CONFIG_MANAGER.pageSize);
            $.ajax({
                type: "GET",
                url: requestUrl,
                async: true,
                headers: {"token": _user.token},
                beforeSend: function () {
                    $.showWaiting("等待中");
                }
            }).done(function (data) {
                setTimeout(function () {
                    $.closeWaiting();
                }, 500);
                _thispage.loadList(JSON.parse(data), _query.tip);
            });
        },
        //加载列表
        loadList: function (data, tip) {
            var src, tips, id;
            if (data && data.length > 0) {
                $.each(data.data, function (i, item) {
                    src = "";
                    tips = "", id = 0;
                    if (item.goods) {
                        tips = "goods"
                        src = ( item.goods.logo ) ? CONFIG_MANAGER.imgeUrl + item.goods.logo : null;
                        id = item.goods.sku_id;
                    } else if (item.shop) {
                        tips = "shop"
                        src = ( item.shop.logo ) ? CONFIG_MANAGER.imgeUrl + item.shop.logo : null;
                        id = item.shop.id;
                    }
                    if (!src) {
                        src = "../../images/dlogo.png";
                    }
                    tips += ( "|" + item.related_id + "|" + id );
                    //				if( length == (i+1) ){
                    //					html.push('<div class="with100 overflow">');
                    //				}else{
                    _html.push('<div class="mbbottom with100 overflow">');
                    //				}
                    _html.push('<dl class="cpxqxztop cpxqxztop1 mtop0">');
                    if ("trace" != tip) {
                        _html.push('<p name="delAttention" tip="' + tips + '" class="qxguanzhu">-</p>');
                    }
                    _html.push('<dt name="favLink" tip="' + tips + '"><img src="' + src + '"></dt>');
                    _html.push('<dd name="favLink" tip="' + tips + '" class="wdgzwz qhuise">' + item.title + '</dd>');
                    _html.push('<dd name="favLink" tip="' + tips + '" class="wlxxlc wlxxlc5"><span><b>' + toDateString(item.create_time, "yyyy-MM-dd hh:mm") + '</b></span></dd>');
                    _html.push('</dl>');
                    _html.push('</div>');
                });
                $("#attentionList").html(_html.join(''));
            } else {
                $("#attentionList").html('<div class="zhanwushuju"><img src="../../images/zanwushuju.png"><br>暂无数据</div>');
            }
            setTimeout(function () {
                _myscroll.refresh();
                _pullUp.hide();
                _pullDown.hide();
            }, 100);
        },
        //页面事件
        bindEvent: function () {
            //点击回退
            $("#back").on("tap", function () {
                back();
            });

            //点击“关注的宝贝”，“关注的店铺”，“我的足迹”
            $("li[name='mattention']").on("tap", function () {
                var tip = $(this).attr("tip"), value = "translateX(" + $(this).offset().left + "px)";
                $("#bottowtab").css({
                    "margin-left": "0px",
                    "transform": value,
                    "-moz-transform": value,
                    "-webkit-transform": value,
                    "-o-transform": value,
                    "-ms-transform": value
                });
                _query.tip = tip;
                _thispage.loadAttentions();
            });
            //点击跳转
            $("#attentionList").on("click", "dd[name='favLink']", function () {
                var tips = $(this).attr("tip").split("|");
                if ("goods" == tips[0]) {
                    redirect("../goods/goodsdetail.html?id=" + tips[2]);
                    return false;
                } else {
                    redirect("../shop/shop.html?sku_id=" + tips[2]);
                    return false;
                }
            });
            //点击删除
            $("#attentionList").on("click", "p[name='delAttention']", function () {
                var attention = $(this),
                    tips = attention.attr("tip").split("|"),
                    requestUrl = CONFIG_MANAGER.RequstUrl + "v1/member/favorite/" + tips[0] + "/" + tips[1];

                $.confirm("取消关注?", function (e) {
                    if (e == 0) {
                        $.ajax({
                            type: "DELETE",
                            url: requestUrl,
                            async: true,
                            headers: {"token": _user.token},
                        }).done(function (data) {
                            if (data) {
                                $.toast(JSON.parse(data).message);
                                attention.parent().parent().remove();
                            }
                        });
                    }
                });
            });
        },


    };
    _thispage = new attentionPage();
});





